package com.tedu;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class ConsumerBlockDemo {
//    定义阻塞队列
    static BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
    public static void main(String[] args) {
//        自定义线程表示消费者线程--守护线程
       Thread consumer =  new Thread(){
            @Override
            public void run() {
//          一直从队列中取出数据并输出
            while (true){

                try {
                    String str =queue.take();
                    System.out.println("取出的元素是："+str);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            }
        };
            consumer.setDaemon(true);
            consumer.start();
//        main线程表示生产者线程
        for (int i = 1; i <=10; i++) {
            try {
                TimeUnit.SECONDS.sleep(2);
                queue.put("hello"+i);
                System.out.println(queue);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
